package day19.feifei;

public class DeadLock implements Runnable{
    private Integer i = 1;
    private Object o1 = new Object();
    private Object o2 = new Object();
    @Override
    public void run() {
        while(true){
            i++;
            if(i%2 == 0){
                synchronized (o1) {
                    System.out.println("lock obj1");
                    synchronized (o2) {System.out.println("lock obj2");
                    }
                }
            }else{
                synchronized (o2) {
                    System.out.println("lock obj2");
                    synchronized (o1) {
                        System.out.println("lock obj1");
                    }
                }
            }
        }
    }
}

class DeadLockDemo {
    public static void main(String[] args) {
        DeadLock dl = new DeadLock();
        Thread t1 = new Thread(dl);
        Thread t2 = new Thread(dl);
        t1.start();
        t2.start();
    }
}